package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.dto.IntegralRulesDTO;
import com.yuanfeng.base.entity.BaseIntegralRulesEntity;
import com.yuanfeng.base.mapper.BaseIntegralRulesMapper;
import com.yuanfeng.base.service.BaseIntegralRulesService;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class BaseIntegralRulesServiceImpl extends ServiceImpl<BaseIntegralRulesMapper, BaseIntegralRulesEntity>
    implements BaseIntegralRulesService{

    @Override
    public ResponseResult queryIntegralRules() {
        BaseIntegralRulesEntity integralRules = new BaseIntegralRulesEntity();
        try {
            integralRules = this.baseMapper.selectOne(new QueryWrapper<BaseIntegralRulesEntity>());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success(integralRules);
    }

    @Override
    public ResponseResult integralRulesAddOrUpdate(IntegralRulesDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            if (null == param.getId()) {
                this.baseMapper.integralRulesAdd(param);
            } else {
                this.baseMapper.update(null, new LambdaUpdateWrapper<BaseIntegralRulesEntity>()
                        .set(BaseIntegralRulesEntity::getRuleLogin, param.getRuleLogin())
                        .set(BaseIntegralRulesEntity::getRuleRegister, param.getRuleRegister())
                        .set(BaseIntegralRulesEntity::getRuleConsume, param.getRuleConsume())
                        .set(BaseIntegralRulesEntity::getId, param.getId()));
            }
            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }
}




